package com.ggh.jinjilive.ui.mall.order;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityOrderBinding;
import com.ggh.jinjilive.databinding.FragmentOrderBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.Pay.WeChatPayMobile;
import com.ggh.jinjilive.mobile.mall.order.MineOrder;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mall.goods.GoodsViewModel;
import com.ggh.jinjilive.ui.mall.order.adapter.OrderAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.pay.PayUtils;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 我的订单
 */
public class OrderFragment extends BaseFragment<FragmentOrderBinding> implements OnRefreshListener, OnLoadMoreListener{
    //VM
    private OrderViewModel orderViewModel;
    //
    private int status;
    private int type;
    //订单
    private OrderAdapter orderAdapter;

    public OrderFragment(int status,int type){
        this.status = status;
        this.type = type;
    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        orderViewModel = new ViewModelProvider(this,viewModelFactory).get(OrderViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_order;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取订单信息
                orderViewModel.isRefresh = true;
                orderViewModel.orderList(status,type);
            }
        });
        loadService.showCallback(LoadingCallback.class);
    }


    @Override
    public void initView() {
        super.initView();
        orderAdapter = new OrderAdapter();
        orderAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                if(position == 99999){//去付款
                    MineOrder mineOrder = (MineOrder) mObject;
                    if(null != mineOrder){
                        Map<String, Object> map = new HashMap<>();
                        map.put("order_sn",mineOrder.getOrder_sn());
                        map.put("pay_type",mineOrder.getPay_type());
                        if(mineOrder.getPay_type() == 0){//微信
                            orderViewModel.weChatpayOrder(map);
                        }else if(mineOrder.getPay_type() == 1){//支付宝
                            orderViewModel.alipayPayOrder(map);
                        }
                    }
                }
            }
        });
        binding.rvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvGoods.setAdapter(orderAdapter);
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    public void initData() {
        super.initData();
        //获取订单信息
        orderViewModel.isRefresh = true;
        orderViewModel.orderList(status,type);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
        //获取订单信息
        orderViewModel.mMineOrderList.observe(this, new Observer<List<MineOrder>>() {
            @Override
            public void onChanged(List<MineOrder> mineOrders) {
                binding.refreshLayout.finishRefresh();
                if(orderViewModel.isRefresh){
                    if(mineOrders.size() > 0){
                        loadService.showSuccess();
                        orderAdapter.setAdd(mineOrders);
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(mineOrders.size() > 0){
                        orderAdapter.Add(mineOrders);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
        //微信付款信息
        orderViewModel.mWeChatPayMobile.observe(this, new Observer<WeChatPayMobile>() {
            @Override
            public void onChanged(WeChatPayMobile weChatPayMobile) {
                //调起微信
                PayUtils.getInstance(mContext).WeChatPay(weChatPayMobile);
            }
        });
        //支付宝付款信息
        orderViewModel.mAlipayString.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String AlipayPay) {
                //调起支付宝
                PayUtils.getInstance(mContext).AlipayPay(AlipayPay);
            }
        });
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        //获取订单信息
        orderViewModel.isRefresh = true;
        orderViewModel.orderList(status,type);
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        //获取订单信息
        orderViewModel.isRefresh = false;
        orderViewModel.orderList(status,type);
    }

}
